class ProvincesController < ApplicationController
  
  before_filter :require_user
  
  # GET /provinces
  # GET /provinces.xml
  def index
    authorize! :read, Province
    #@provinces = Province.all
    @search = Province.search(params[:search])
    @provinces = @search.paginate(:per_page => 5,:page => params[:page])
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @provinces }
    end
  end

  # GET /provinces/1
  # GET /provinces/1.xml
  def show
    authorize! :read, Province
    @province = Province.find(params[:id])
    @read_only = true
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @province }
    end
  end

  # GET /provinces/new
  # GET /provinces/new.xml
  def new
    authorize! :create, Province
    @province = Province.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @province }
    end
  end

  # GET /provinces/1/edit
  def edit
    authorize! :update, Province
    @province = Province.find(params[:id])
    @read_only = false
  end

  # POST /provinces
  # POST /provinces.xml
  def create
    authorize! :create, Province
    @province = Province.new(params[:province])
    @province.user_log = current_user.login
    respond_to do |format|
      if @province.save
        format.html { redirect_to(@province, :notice => 'Province was successfully created.') }
        format.xml  { render :xml => @province, :status => :created, :location => @province }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @province.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /provinces/1
  # PUT /provinces/1.xml
  def update
    authorize! :update, Province
    @province = Province.find(params[:id])
    @province.user_log = current_user.login
    respond_to do |format|
      if @province.update_attributes(params[:province])
        format.html { redirect_to(@province, :notice => 'Province was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @province.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /provinces/1
  # DELETE /provinces/1.xml
  def destroy
    authorize! :destroy , Province
    @province = Province.find(params[:id])
    @province.user_log = current_user.login
    @province.destroy

    respond_to do |format|
      format.html { redirect_to(provinces_url) }
      format.xml  { head :ok }
    end
  end
end
